package com.odm.communitymanage.ui.health

import android.graphics.Color
import android.opengl.Visibility
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.RadioButton
import androidx.core.view.get
import androidx.lifecycle.ViewModelProviders
import androidx.recyclerview.widget.LinearLayoutManager
import com.blankj.utilcode.util.*
import com.odm.communitymanage.R
import com.odm.communitymanage.base.BaseMVVMFragment
import com.odm.communitymanage.core.Constants
import kotlinx.android.synthetic.main.fragment_health.*

/**
 * @description: 健康管理页面
 * @author: ODM
 * @date: 2020/4/14
 */
class HealthFragment : BaseMVVMFragment()  {

    private var temperatureAdapter : HealthOptionsAdapter ?= null
    private var sickAdapter : HealthOptionsAdapter ?= null

    private var viewModel: HealthViewModel ?= null


    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        viewModel = ViewModelProviders.of(this).get(HealthViewModel::class.java)
        return super.onCreateView(inflater, container, savedInstanceState)
    }



    override fun initViews() {
        tvUserName_Health.text = SPUtils.getInstance(Constants.SP_USER).getString(Constants.USER_NAME , getString(R.string.unknown))

        initHealthOptionsRecyclerView()

        //返回键事件
        ivBack_Health.setOnClickListener {
            pop()
        }

        //一键提交按钮点击事件
        btnDirectPost_Health.setOnClickListener {
            //通知用户成功
            showSuccessSnack(getString(R.string.post_success))
        }

        //提交选项按钮点击事件
        btnPostHealthInfo.setOnClickListener {
            //强制选中信息后才可以提交信息
            if(viewModel?.temperatureOption == -1 || viewModel?.sickSelected?.isEmpty() == true){
                ToastUtils.showShort(getString(R.string.please_select_information_to_be_submitted))
                return@setOnClickListener
            }
            //假数据,省略网络请求，自动改状态
            checkTemperature()
            //通知用户成功
            showSuccessSnack(getString(R.string.post_success))
        }

    }

    /**
     * 初始化健康选项 列表
     */
    private fun initHealthOptionsRecyclerView(){
        /**
         * 初始化 体温选择列表
         */
        rvTemperatureOptions.layoutManager = LinearLayoutManager(requireContext())
        temperatureAdapter = viewModel?.temperatureList?.let {
            HealthOptionsAdapter(it)
        }
        rvTemperatureOptions.adapter  = temperatureAdapter

        //体温列表的item点击事件，选择策略：每一项都与其他项互斥
        temperatureAdapter?.setOnItemClickListener { adapter, view, position ->
            //将此项设为被选择项，其他项设为不选择
            temperatureAdapter?.data?.get(position)?.isSelected = true
            if(viewModel?.temperatureOption != -1){
                //已存在被选的单项，取消它的选中状态
                val selected : RadioButton?= view.findViewById(R.id.checkbox_health_option) as RadioButton
                selected?.isChecked  = false
                temperatureAdapter?.data?.get(viewModel?.temperatureOption ?: 0)?.isSelected = false
            }
            val toSelect : RadioButton ?= view.findViewById(R.id.checkbox_health_option) as RadioButton
            toSelect?.isChecked = true
            //修改当前被选择 的 体温
            viewModel?.temperatureOption = position

            temperatureAdapter?.notifyDataSetChanged()
        }

        /**
         * 初始化 症状选择列表
         */
        rvSickOptions.layoutManager = LinearLayoutManager(requireContext())
        sickAdapter = viewModel?.sickList?.let {
            HealthOptionsAdapter(it)
        }
        rvSickOptions.adapter  = sickAdapter
        //选择策略：第0项代表无症状，第1-n项代表不同症状，无症状与其他项是互斥的。其他症状可以多选提交
        sickAdapter?.setOnItemClickListener { adapter, view, position ->
            if(position == 0){
                if(viewModel?.sickSelected?.isNotEmpty() == true){
                    //已有选择，需要重置它们的状态
                    for ( p in viewModel?.sickSelected!!){
                        sickAdapter?.data?.get(p)?.isSelected = false
                    }
                    //选择 了 “无” ，清空下面的选项后，补上 0
                    viewModel?.sickSelected?.clear()
                }
                viewModel?.sickSelected?.add(0)
                sickAdapter?.data?.get(0)?.isSelected = true
            }else {
                if(viewModel?.sickSelected?.contains(0) == true){
                    //如果当前已选择了 “无”，则取消掉无症状选择
                    viewModel?.sickSelected?.remove(0)
                    sickAdapter?.data?.get(0)?.isSelected = false
                }
                viewModel?.sickSelected?.add(position)
                sickAdapter?.data?.get(position)?.isSelected = true
            }
            //刷新数据
            adapter.notifyDataSetChanged()
        }

    }





    override fun getLayoutId(): Int {
        return R.layout.fragment_health
    }


    override fun initViewModelObserve() {

    }



    /**
     * 假数据，自动根据提交改当前状态
     */
    fun checkTemperature(){
        when(viewModel?.temperatureOption){
            0 -> {
                ll_bg_Health.background =resources.getDrawable(R.drawable.shape_frame_green_no_radius)
                tvState_Health.text = viewModel?.temperatureList?.get(0)?.situation
                rlContainer_VisitorCard.background = resources.getDrawable(R.color.health_green)
                btnDirectPost_Health.visibility = View.GONE
            }

            1 -> {
                ll_bg_Health.background =resources.getDrawable(R.drawable.shape_frame_orange_no_raidus)
                tvState_Health.text = viewModel?.temperatureList?.get(1)?.situation
                rlContainer_VisitorCard.background = resources.getDrawable(R.color.health_yellow)
                btnDirectPost_Health.visibility = View.GONE
            }
            2->{
                ll_bg_Health.background =resources.getDrawable(R.drawable.shape_frame_orange_no_raidus)
                tvState_Health.text = viewModel?.temperatureList?.get(2)?.situation
                rlContainer_VisitorCard.background = resources.getDrawable(R.color.health_yellow)
                btnDirectPost_Health.visibility = View.GONE

            }
            3->{
                ll_bg_Health.background =resources.getDrawable(R.drawable.shape_frame_red_no_radius)
                tvState_Health.text = viewModel?.temperatureList?.get(3)?.situation
                rlContainer_VisitorCard.background = resources.getDrawable(R.color.health_red)
                btnDirectPost_Health.visibility = View.GONE

            }
        }
    }

}